import { zg99Request } from "/utils/requests";
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showContact: true,
        suspendShow: true,
        showIcon: true,
        bg1color: '',
        bookDetails_box: !1,
        isSydw: false,
        myPhone: "",
        showIcon: !0,
        isShowAuth: !1,
        yl: 1,
        ziliaobox: 0,
        isSydw: false,
        login: true,
        chaxunbox: 0,
        islogin: 0,
        bgcolor: '#fff',
        color: '#000',
        opacity: '0.6',
        bottom: '30%',
        imgUrl: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        xhs.showLoading({
            title: '页面加载中',
            mask: true
        })
        xhs.showShareMenu({
          shareTypes: 'normal',
        })
        this.setData(await app.methods.myPhone()); // 获取本地存储手机号
        if(!this.data.myPhone) {
            await app.methods.getPhoneNumber()
            this.setData({
              isShowAuth: !0
            })
        }
        let links, jxOptions;
        if (options.scene) {
            options.scene = options.scene.replace(/%253D/g, ".");
            options.scene = options.scene.replace(/%2526/g, "-");
            jxOptions = await app.methods.getOPtions(options);
        }
        let id = options.scene ? jxOptions.id : options.id;
        options.id = id;
        links = await app.methods.getLink(id);
        options.yl = links.yl ? links.yl : '';
        options.xm = links.xm ? links.xm : '';
        if(links.actid) options.actid = links.actid;
        options.fx = options.scene ? jxOptions.fx : options.fx;
        options.gh = options.scene ? jxOptions.gh : options.gh;
        this.setData({
            options: options
        })
        if (options.xm != undefined) {
            this.setData({
                xm: options.xm
            })
            let xiangmu = options.xm
            if (xiangmu.indexOf('sydw') >= 0) {
                this.setData({
                    isSydw: true
                })
            }
        }
        if (options.yl != undefined) {
            if (options.yl.length > 0) {
                this.setData({
                    yl: Number(options.yl)
                })
            }
        }
        await this.getHdInfo(this.data.options.xm)
        this.setData(await app.methods.getIdentity(options)); // 获取个人身份信息
        if(this.data.getIdentity.crm) {
            options.wxh = this.data.getIdentity.crm
        } else {
            options.wxh = options.gh
        }
        this.setData(await app.methods.getWechat(options)); // 获取个人身份信息
        this.setData(await app.methods.guishudi(options)); // 获取归属地
        let fenbu = this.data.fenbu;
        let fenxiao = this.data.fenxiao;
        let getIdentity = this.data.getIdentity;
        getIdentity.fenbu = fenbu;
        getIdentity.fenxiao = fenxiao;
        getIdentity.zzname = this.data.zzname;
        getIdentity.zzid = this.data.zzid;
        getIdentity.erpdqm = this.data.erpdqm;
        this.setData({
            getIdentity: getIdentity
        })
        this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, this.data.type))
        this.thisMethod()
        // console.log(this.data)
        xhs.hideLoading()
    },
    // 加载内容
    async getHdInfo(xiangmu) {
        return new Promise((resolve) => {
            zg99Request({
                url: '/index/chaxun/getfzinfo/?actid=47204&callback=&',
                method: 'GET',
                dataType: 'jsonp',
                data: {
                    xiangmu: xiangmu,
                    shijian: Date.parse(new Date()) / 1000
                }
            }).then(res => {
                let newStr = app.methods.getAPIReturn(res);
                // console.log(newStr)
                if (newStr.status == 1) {
                    this.setData({
                        src: newStr.lists[0].src,
                        href: newStr.lists[0].href,
                        button: newStr.lists[0].button,
                        bottom: !newStr.lists[0].bottom ? this.data.bottom : newStr.lists[0].bottom,
                        type: newStr.lists[0].type,
                        color: !newStr.lists[0].color ? this.data.color : newStr.lists[0].color,
                        bgcolor: !newStr.lists[0].bgcolor ? this.data.bgcolor : newStr.lists[0].bgcolor,
                        opacity: !newStr.lists[0].opacity ? this.data.opacity : newStr.lists[0].opacity,
                        imgUrl: !newStr.lists[0].imgUrl ? this.data.imgUrl : newStr.lists[0].imgUrl,
                        laiyuan: newStr.lists[0].laiyuan + 'xhs',
                        form: this.data.options.actid ? Number(this.data.options.actid) : Number(newStr.lists[0].form),
                        notice: newStr.lists[0].notice,
                    })
                    app.globalData.laiyuan = `${newStr.lists[0].laiyuan}`
                }
                resolve()
            }).catch(err => {
                console.log(err)
                resolve()
            })
        })
    },
    // 提交信息
    async zhuce() {
        if ( !this.data.myPhone) {
            this.setData({
                isShowAuth: true
            })
            return false
        }
        if (!this.data.user) {
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, this.data.type))
            this.thisMethod()
        }
        if (!this.data.href && this.data.notice) {
            xhs.showModal({
                title: '提示',
                content: this.data.notice
            })
        } else {
            let newHref = '';
            newHref = this.data.href + (this.data.myPhone ? '?phone=' + this.data.myPhone : '')
            newHref = newHref + (this.data.options.gonghao ? '&gonghao=' + this.data.options.gonghao : '')
            newHref = newHref + (this.data.options.fenxiao ? '&fenxiao=' + this.data.options.fenxiao : '')
            newHref = newHref + (this.data.options.wxh ? '&wxh=' + this.data.options.wxh : '')
            newHref = newHref + (this.data.options.xm ? '&xm=' + this.data.options.xm : '')
            xhs.navigateTo({
                url: '/packageA/zt/new?href=' + newHref,
            })
        }
    },
    // 该怎么就怎么
    async thisMethod(type) {
        if (!this.data.user) {
            if ((this.data.isSydw || this.data.isYiShi) && this.data.myPhone != '') {
                this.setData({
                    bookDetails_box: 1
                })
                return false
            } else if ((!this.data.isSydw || !this.data.isYiShi) && this.data.myPhone) {
                await app.methods.registerCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, '', '', this.data.book, this.data.type)
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, this.data.type))
            }
        } else {
            if ((this.data.user.user.gwlb == '' || this.data.user.user.gwlb == 'null' || !this.data.user.user.gwlb) && (this.data.isSydw || this.data.isYiShi)) {
                this.setData({
                    bookDetails_box: 1
                })
                return false
            } else {
                await app.methods.updateuserCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.user.user.gwlb, '', this.data.type, this.data.book, this.data.user.user)
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, this.data.type))
            }
        }
        // console.log(this.data)
    },
    // 选择岗位
    async choiceGw(e) {
        // console.log(this.data)
        if (!this.data.user) {
            await app.methods.registerCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, e.currentTarget.dataset.gwlb, '', this.data.book, this.data.type)
            this.setData({
                bookDetails_box: !1
            })
            // this.go()
        } else {
            await app.methods.updateuserCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, e.currentTarget.dataset.gwlb, '', this.data.type, this.data.book, this.data.user.user)
            this.setData({
                bookDetails_box: !1
            })
            // this.go()
        }
    },
    // 监听手机号
    async watchBack(value) {
        this.setData({
            myPhone: value.myPhone
        })
        if (value && value.myPhone) {
            this.setData(await app.methods.getphonestatusCX(value.myPhone, this.data.form, this.data.type))
            await this.thisMethod();
            if (!this.data.href && this.data.notice) {
                xhs.showModal({
                    title: '提示',
                    content: this.data.notice
                })
            } else {
                let newHref = '';
                newHref = this.data.href + (this.data.myPhone ? '&phone=' + this.data.myPhone : '')
                newHref = newHref + (this.data.options.gonghao ? '&gonghao=' + this.data.options.gonghao : '')
                newHref = newHref + (this.data.options.fenxiao ? '&fenxiao=' + this.data.options.fenxiao : '')
                newHref = newHref + (this.data.options.wxh ? '&wxh=' + this.data.options.wxh : '')
                newHref = newHref + (this.data.options.xm ? '&xm=' + this.data.options.xm : '')
                newHref.replace(/&amp;/g, "&")
                // console.log('/packageA/zt/new?href=' + newHref)
                xhs.navigateTo({
                    url: '/packageA/zt/new?href=' + newHref,
                })
            }
        }
    },
    async user_login(e){
        if(typeof e.detail == 'boolean') {
            this.setData({
                isShowAuth: e.detail
            })
        } else {
            this.setData({
                myPhone: e.detail
            })
            this.setData(await app.methods.getphonestatusCX(value.myPhone, this.data.form, this.data.type))
            await this.thisMethod();
            if (!this.data.href && this.data.notice) {
                xhs.showModal({
                    title: '提示',
                    content: this.data.notice
                })
            } else {
                let newHref = '';
                newHref = this.data.href + (this.data.myPhone ? '&phone=' + this.data.myPhone : '')
                newHref = newHref + (this.data.options.gonghao ? '&gonghao=' + this.data.options.gonghao : '')
                newHref = newHref + (this.data.options.fenxiao ? '&fenxiao=' + this.data.options.fenxiao : '')
                newHref = newHref + (this.data.options.wxh ? '&wxh=' + this.data.options.wxh : '')
                newHref = newHref + (this.data.options.xm ? '&xm=' + this.data.options.xm : '')
                newHref.replace(/&amp;/g, "&")
                // console.log('/packageA/zt/new?href=' + newHref)
                xhs.navigateTo({
                    url: '/packageA/zt/new?href=' + newHref,
                })
            }
        }
    },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let { fx = 'hbwtb', gh = 'zcj37368', id } = this.data.options;
        let { imgUrl, notice, laiyuan } = this.data;
        console.log(imgUrl, notice, laiyuan)
        console.log(`/packageA/zt/zt?id=${id}&fx=${fx}&gh=${gh}&wxh=${this.data.options.wxh}`)
        return {
            title: notice ? notice : laiyuan,
            path: `/packageA/zt/zt?id=${id}&fx=${fx}&gh=${gh}&wxh=${this.data.options.wxh}`,
            imageUrl: imgUrl ? imgUrl : '',
        }
    }
})